package com.zll.dms.async;

import com.zll.dms.exception.MyBaseException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;

/**
 * 异步处理服务
 */
@Deprecated
@Slf4j
@Service
public class AsyncService {


    /**
     * 异步发送邮件
     * @param javaMailSender 邮件发送对象
     * @param from 发送者
     * @param toEmail 接受者
     * @param subject 主题
     * @param content 内容
     */
    @Async
    public void asyncSendEmail(JavaMailSender javaMailSender,String from,String toEmail, String subject, String content) {
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        try {
            MimeMessageHelper messageHelper = new MimeMessageHelper(mimeMessage,true,"UTF-8");
            messageHelper.setFrom(from);
            messageHelper.setTo(toEmail);
            messageHelper.setSubject(subject);
            messageHelper.setText(content,true);
            javaMailSender.send(mimeMessage);
            log.info("发送成功。。。。");
        } catch (MessagingException e) {
            throw new MyBaseException("发送邮件失败请重试");
        }
    }
}
